package com.situ.web.service.impl;

import java.util.List;

import com.situ.web.dao.ICourseDao;
import com.situ.web.dao.impl.CourseDaoImpl;
import com.situ.web.entity.Course;
import com.situ.web.service.ICourseService;
import com.situ.web.util.JSONResult;
import com.situ.web.vo.PageInfo;
import com.sun.org.apache.regexp.internal.recompile;

public class CourseServiceImpl implements ICourseService{
	private ICourseDao courseDao = new CourseDaoImpl();

	@Override
	public List<Course> selectAll() {
		List<Course> list = courseDao.selectAll();
		for (Course course : list) {
			course.setName("2020" + course.getName());
		}
		return list;
	}

	@Override
	public boolean insert(Course course) {
		int count = courseDao.insert(course);
//		if (count == 1) {
//			return true;
//		} else {
//			return false;
//		}
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = courseDao.deleteById(id);
		return count == 1;
	}

	@Override
	public PageInfo selectByPage(int pageNo, int pageSize) {
		// 在这个service层代码里面就是来封装PageInfo这个对象
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(pageNo);
		pageInfo.setPageSize(pageSize);
		
		// List<Course> list; 当前页数据集合
		int offset = (pageNo - 1) * pageSize;
		List<Course> list = courseDao.selectByPage(offset, pageSize);
		// pageInfo.setData(list);
		pageInfo.setList(list);
		// 一共有多少页
		// 首先查询总的数量
		int totalCount = courseDao.selectTotalCount();
		int totalPage = (int)Math.ceil((double)totalCount / pageSize);
		pageInfo.setTotalPage(totalPage);
		
		return pageInfo;
	}
	
	public static void main(String[] args) {
		System.out.println(3 / 2);// 1
		System.out.println(Math.ceil(3 / 2));// 1.0
		System.out.println(Math.ceil((double)3 / 2));// 2.0
	}

	@Override
	public JSONResult selectLayuiByPage(int page, int limit) {
		int offset = (page - 1) * limit;
		List<Course> list = courseDao.selectByPage(offset, limit);
		// 首先查询总的数量
		int totalCount = courseDao.selectTotalCount();
		
		// return JSONResult.ok(list, totalCount);
		
		JSONResult jsonResult = new JSONResult(JSONResult.OK, list, totalCount);
		return jsonResult;
	}
	
}
